import '../entity/game.dart';
import '../entity/view_models/game_detail_data.dart';
import 'game_repository.dart';
import 'user_product_repository.dart';
import 'wishlist_repository.dart';

class DataService {
  final GameRepository _gameRepository;
  final WishlistRepository _wishlistRepository;
  final UserProductRepository _userProductRepository;

  WishlistRepository get wishlistRepository => _wishlistRepository;

  DataService({
    required GameRepository gameRepository,
    required WishlistRepository wishlistRepository,
    required UserProductRepository userProductRepository,
  })  : _gameRepository = gameRepository,
        _wishlistRepository = wishlistRepository,
        _userProductRepository = userProductRepository;


  DataService.standard() :
        _gameRepository = GameRepository(),
        _wishlistRepository = WishlistRepository(),
        _userProductRepository = UserProductRepository();


  //为游戏详情页获取所有需要的数据包
  Future<GameDetailData?> fetchGameDetailPackage({
    required String gameId,
    required String userId,
  }) async {

    final results = await Future.wait([
      _gameRepository.getGameWithProducts(gameId),
      _wishlistRepository.isGameInWishlist(userId, gameId),
      _userProductRepository.isGameOwnedByUser(userId: userId, gameId: gameId),
    ]);

    //从results列表中解析出各个查询的结果
    final game = results[0];
    final isInWishlist = results[1] as bool;
    final isOwned = results[2] as bool;

    //如果游戏本身不存在，则无法构建数据包，返回null
    if (game == null) {
      return null;
    }

    //组合数据并返回
    return GameDetailData(
      game: game as Game,
      isInWishlist: isInWishlist,
      isOwned: isOwned,
    );
  }


}